/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javalens.featurecalculators;

import javalens.astronomy.Event;
import javalens.astronomy.LightCurve;
import javalens.*;
import javalens.featurecalculators.FeatureCalculator;
import javalens.util.CalculationException;
import javalens.util.Util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/** This class returns a single lens fit result AND five polynomial fits
 * to the single lens fit residuals.
 *
 * @author vermaak
 */
public class SingleLensAndResidualsFeatureCalculator implements FeatureCalculator {
    SingleLensFitFeatureCalculator singleLensCalculator;

    public SingleLensAndResidualsFeatureCalculator() {
        this.singleLensCalculator = new SingleLensFitFeatureCalculator();
    }
    
    public String[] getHeaders() {
        String[] fitHeaders = singleLensCalculator.getHeaders();
        String[] ownHeaders = new String[]{
            "logResidualLineChisq", "residualOffset", "residualSlope",
            "logResidualParabolaChisq", "residualParabolaYAxis",
            "logResidualCubicChisq",
            "logResidualQuarticChisq",
            "logResidualQuinticChisq"
        };
        List<String> ret = new ArrayList<String>(Arrays.asList(fitHeaders));
        ret.addAll(Arrays.asList(ownHeaders));
        return ret.toArray(new String[ret.size()]);
    }

    public double[] getValues(LightCurve curve) throws CalculationException {
        double[] fitValues = singleLensCalculator.getValues(curve);
        Event fittedEvent = singleLensCalculator.getFitResultEvent();
        LightCurve fittedCurve = LightCurve.createFromTimeArray(curve.getDays(), fittedEvent);
        // now set up the difference curves
        double[] x = fittedCurve.getDays();
        double[] dataY = curve.getMags();
        double[] fittedY = fittedCurve.getMags();
        double[] diffs = new double[x.length];
        for (int i=0;i<x.length;++i) {
            diffs[i] = fittedY[i] - dataY[i];
        }
        double[] ownValues = Util.fitFivePolynomials(x, diffs);
        // combine and return
        double[] ret = new double[fitValues.length + ownValues.length];
        int i=0;
        for (double d : fitValues) {
            ret[i++] = d;
        }
        for (double d : ownValues) {
            ret[i++] = d;
        }
        return ret;
    }

}
